package de.lunaplayer.view.bars;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.*;

import de.lunaplayer.app.AppStarter;


@SuppressWarnings("serial")
public class ToolbarPanel extends JPanel{
	
	private JToolBar toolbar;
	private JButton open;
	private JButton save;
	private JButton pref;
	private JButton radio;
	private JButton playlist;
	private JButton podcast;
	private JButton help;
	private JButton quit;
	private JLabel titel;
	private JLabel pattern;
	private JTextField searchField;
	
	private File playlistfile;
	
	public ToolbarPanel(){
		initialize();
		initActions();
		setTooltips();
		buildToolbar();
	}
	
	private void initialize(){
		this.setLayout(new BorderLayout());
		this.toolbar = new JToolBar();
		this.open = new JButton(AppStarter.makeIcon("/resources/document-open.png")); //$NON-NLS-1$
		this.save = new JButton(AppStarter.makeIcon("/resources/document-save.png")); //$NON-NLS-1$
		this.pref = new JButton(AppStarter.makeIcon("/resources/stock_music-library.png")); //$NON-NLS-1$
		this.playlist = new JButton(AppStarter.makeIcon("/resources/playlist-new.png")); //$NON-NLS-1$
		this.podcast = new JButton(AppStarter.makeIcon("/resources/podcast-new.png")); //$NON-NLS-1$
		this.radio = new JButton(AppStarter.makeIcon("/resources/internet-radio-new.png")); //$NON-NLS-1$
		this.help = new JButton(AppStarter.makeIcon("/resources/help-faq.png")); //$NON-NLS-1$
		this.quit = new JButton(AppStarter.makeIcon("/resources/AppStarterlication-exit.png")); //$NON-NLS-1$
		this.titel = new JLabel(AppStarter.makeIcon("/resources/mp3player.png")); //$NON-NLS-1$
		this.pattern = new JLabel("  Suche:  ");
		this.searchField = new JTextField();
	}
	
	private void initActions() {
		this.open.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				openFile();	
			}
		});
		this.save.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				saveFile();	
			}
		});
		this.pref.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				openMusic();	
			}
		});
		this.radio.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				getInternetRadio();	
			}
		});
		this.podcast.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				getPodcast();	
			}
		});
		this.playlist.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				//initInternalFrame();	
			}
		});
		this.help.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				openHelp();	
			}
		});
		this.quit.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);	
			}
		});
		
	}
	protected void openHelp() {
		// TODO Tutvideo mittels wink erstellen und über Browser abspielen lassen
		
	}
	protected void getPodcast() {
		// TODO Podcast implementieren
		
	}
	protected void getInternetRadio() {
		// TODO Internetradio implementieren
		
	}
	protected void openMusic() {
		// TODO Musicordner implementieren
		
	}
	protected void saveFile() {
		JFileChooser fc = new JFileChooser();
		int retvalue = fc.showSaveDialog(this);
		if(retvalue == JFileChooser.APPROVE_OPTION){
			fc.setSelectedFile(playlistfile);
		}
		
	}
	protected void openFile() {
		JFileChooser fc = new JFileChooser();
		int retvalue = fc.showOpenDialog(this);
		if(retvalue == JFileChooser.APPROVE_OPTION){
			playlistfile = fc.getSelectedFile();
		}
		
	}
	private void setTooltips(){
		this.open.setToolTipText("Datei öffnen"); //$NON-NLS-1$
		this.save.setToolTipText("Datei speichern"); //$NON-NLS-1$
		this.pref.setToolTipText("Einstellungsmenu öffnen"); //$NON-NLS-1$
		this.playlist.setToolTipText("Playlist öffnen"); //$NON-NLS-1$
		this.radio.setToolTipText("Radio Kanäle streamen"); //$NON-NLS-1$
		this.help.setToolTipText("Hilfe öffnen"); //$NON-NLS-1$
		this.quit.setToolTipText("Anwendung beenden"); //$NON-NLS-1$
	}
	private void buildToolbar(){
		this.toolbar.add(open);
		this.toolbar.add(save);
		this.toolbar.add(pref);
		this.toolbar.add(radio);
		this.toolbar.add(playlist);
		this.toolbar.add(podcast);
		this.toolbar.add(pattern);
		this.toolbar.add(searchField);
		this.toolbar.add(new JLabel(AppStarter.makeIcon("/resources/menubar.png"))); //$NON-NLS-1$
		this.toolbar.add(new JLabel(AppStarter.makeIcon("/resources/menubar.png"))); //$NON-NLS-1$
		this.toolbar.add(titel);
		this.toolbar.add(new JLabel(AppStarter.makeIcon("/resources/menubar.png"))); //$NON-NLS-1$
		this.toolbar.add(new JLabel(AppStarter.makeIcon("/resources/menubar.png"))); //$NON-NLS-1$
		this.toolbar.add(help);
		this.toolbar.add(quit);
		add(toolbar);
	}
}
